/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.service.impl;

import me.zhengjie.modules.biz.domain.BizTiktok;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.biz.repository.BizTiktokRepository;
import me.zhengjie.modules.biz.service.BizTiktokService;
import me.zhengjie.modules.biz.service.dto.BizTiktokDto;
import me.zhengjie.modules.biz.service.dto.BizTiktokQueryCriteria;
import me.zhengjie.modules.biz.service.mapstruct.BizTiktokMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author shaocl
* @date 2023-01-14
**/
@Service
@RequiredArgsConstructor
public class BizTiktokServiceImpl implements BizTiktokService {

    private final BizTiktokRepository bizTiktokRepository;
    private final BizTiktokMapper bizTiktokMapper;

    @Override
    public Map<String,Object> queryAll(BizTiktokQueryCriteria criteria, Pageable pageable){
        Page<BizTiktok> page = bizTiktokRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(bizTiktokMapper::toDto));
    }

    @Override
    public List<BizTiktokDto> queryAll(BizTiktokQueryCriteria criteria){
        return bizTiktokMapper.toDto(bizTiktokRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public BizTiktokDto findById(String id) {
        BizTiktok bizTiktok = bizTiktokRepository.findById(id).orElseGet(BizTiktok::new);
        ValidationUtil.isNull(bizTiktok.getId(),"BizTiktok","id",id);
        return bizTiktokMapper.toDto(bizTiktok);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BizTiktokDto create(BizTiktok resources) {
        resources.setId(IdUtil.simpleUUID()); 
        return bizTiktokMapper.toDto(bizTiktokRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(BizTiktok resources) {
        BizTiktok bizTiktok = bizTiktokRepository.findById(resources.getId()).orElseGet(BizTiktok::new);
        ValidationUtil.isNull( bizTiktok.getId(),"BizTiktok","id",resources.getId());
        bizTiktok.copy(resources);
        bizTiktokRepository.save(bizTiktok);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            bizTiktokRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<BizTiktokDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (BizTiktokDto bizTiktok : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名称", bizTiktok.getName());
            map.put("IMEI", bizTiktok.getImei());
            map.put("是否启用", bizTiktok.getEnabled());
            map.put("停用时间", bizTiktok.getEndTime());
            map.put("创建者", bizTiktok.getCreateBy());
            map.put("更新者", bizTiktok.getUpdateBy());
            map.put("创建时间", bizTiktok.getCreateTime());
            map.put("更新时间", bizTiktok.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}